(function() {
  var __hasProp = {}.hasOwnProperty,
    __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };

  if (!window.Moshu) {
    window.Moshu = new Object();
  }

  Moshu.Canvas2D.CanvasImage = (function(_super) {

    __extends(CanvasImage, _super);

    function CanvasImage(src, x, y, width, height) {
      this.src = src;
      this.x = x;
      this.y = y;
      this.width = width;
      this.height = height;
      CanvasImage.__super__.constructor.call(this, this.x, this.y, this.width, this.height);
      this.image = new Image();
      this.image.src = this.src;
      this.sprites = [];
      this.currentSprite = null;
      this.variableWidth = true;
    }

    CanvasImage.prototype.registerSprite = function(name, x, y, width, height) {
      var sprite;
      sprite = {
        name: name,
        x: x,
        y: y,
        width: width,
        height: height
      };
      return this.sprites.push(sprite);
    };

    CanvasImage.prototype.selectSprite = function(sprite) {
      this.currentSprite = sprite;
      if (this.variableWidth) {
        this.width = this.height / sprite.height * sprite.width;
      }
      return this.currentSprite;
    };

    CanvasImage.prototype.selectSpriteByIndex = function(idx) {
      if (idx < 0) {
        idx = idx % this.sprites.length;
        idx = this.sprites.length + idx;
      }
      if (idx >= this.sprites.length) {
        idx = idx % this.sprites.length;
      }
      return this.selectSprite(this.sprites[idx]);
    };

    CanvasImage.prototype.selectSpriteByName = function(name) {
      var i, s, _i, _len, _ref;
      _ref = this.sprites;
      for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) {
        s = _ref[i];
        if (s.name === name) {
          this.selectSprite(s);
          break;
        }
      }
      return this.currentSprite;
    };

    CanvasImage.prototype.selectNextSprite = function() {
      var i, s, _i, _len, _ref;
      if (this.currentSprite) {
        _ref = this.sprites;
        for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) {
          s = _ref[i];
          if (s === this.currentSprite) {
            this.selectSpriteByIndex(i + 1);
            break;
          }
        }
      } else {
        this.selectSpriteByIndex(0);
      }
      return this.currentSprite;
    };

    CanvasImage.prototype.selectPrevSprite = function() {
      var i, s, _i, _len, _ref;
      if (this.currentSprite) {
        _ref = this.sprites;
        for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) {
          s = _ref[i];
          if (s === this.currentSprite) {
            this.selectSpriteByIndex(i - 1);
            break;
          }
        }
      } else {
        this.selectSpriteByIndex(0);
      }
      return this.currentSprite;
    };

    return CanvasImage;

  })(Moshu.Canvas2D.Rectangle);

  Moshu.Canvas2D.ImagePainter = (function(_super) {

    __extends(ImagePainter, _super);

    function ImagePainter(context, canvasImage) {
      this.context = context;
      this.canvasImage = canvasImage;
      ImagePainter.__super__.constructor.apply(this, arguments);
      this.scale = new Moshu.Canvas2D.Vector(1, 1);
    }

    ImagePainter.prototype.flipHorizontally = function() {
      return this.scale.x(this.scale.x() * -1);
    };

    ImagePainter.prototype.flipVertically = function() {
      return this.scale.y(this.scale.y() * -1);
    };

    ImagePainter.prototype.resetFlip = function() {
      return this.scale = new Moshu.Canvas2D.Vector(Math.abs(this.scale.x()), Math.abs(this.scale.y()));
    };

    ImagePainter.prototype.draw = function() {
      var posX, posY;
      if (this.context && this.canvasImage) {
        posX = this.canvasImage.x;
        posY = this.canvasImage.y;
        if (this.scale.x() < 0) {
          posX = this.canvasImage.x * this.scale.x() + this.canvasImage.width * this.scale.x();
        }
        if (this.scale.y() < 0) {
          posY = this.canvasImage.y * this.scale.y() + this.canvasImage.height * this.scale.y();
        }
        this.context.save();
        this.context.scale(this.scale.x(), this.scale.y());
        if (this.canvasImage.currentSprite) {
          this.context.drawImage(this.canvasImage.image, this.canvasImage.currentSprite.x, this.canvasImage.currentSprite.y, this.canvasImage.currentSprite.width, this.canvasImage.currentSprite.height, posX, posY, this.canvasImage.width, this.canvasImage.height);
        } else {
          this.context.drawImage(this.canvasImage.image, posX, posY, this.canvasImage.width, this.canvasImage.height);
        }
        return this.context.restore();
      }
    };

    return ImagePainter;

  })(Moshu.Canvas2D.Painter);

}).call(this);
